/**基础配置*/
// var DOMAIN = 'localhost'
var DOMAIN = 'www.genetuan.com';
var PATH = '/';
var EXPIRES = 14;//cookie过期时间
var ipCookiesParam = { expires: EXPIRES, domain: DOMAIN, path: PATH };
var APPSRC = "http://www.genetuan.com/";
// var APPSRC = "http://www.wanglan.site/";
var HOST = 'http://www.genetuan.com:8088/cms';
// var HOST = 'http://wanglan.free.ngrok.cc/cms';
var APPID = 'wx34ebc701fdef41d5';
// var APPID = 'wxb7930b31e4e134c5';//测试公众号
var ALISRC = 'http://genetuan.oss-cn-shenzhen.aliyuncs.com/';
var statusCODE = {
    OK: 2000,//成功
    NOTLOGIN: 3002,//未登录
};
var yjx = angular.module("myapp", [
    //'ngRoute',
    'ui.router',
    //'ngTouch',
    'ipCookie',
    //'swipe',  //滑动模块
    'mobile-angular-ui',
    'ui.bootstrap',
]);

yjx.run(['$rootScope', '$window', '$location', '$log', 'ipCookie', '$timeout', 'browserJudge', 'LoginService', '$init', '$fetch', 'urlCgService', 'modalService',
    function ($rootScope, $window, $location, $log, ipCookie, $timeout, browserJudge, LoginService, $init, $fetch, urlCgService, modalService) {
        if (browserJudge.isWx()) {
            if (browserJudge.userAgent().match('android')) {
                $rootScope.isAndroid = true
            } else {
                $rootScope.isAndroid = false
            }
        } else {
            modalService.mainMsgError("请在微信浏览器打开")
        }

        function goLogin() {
            $rootScope.showReady = false;
            // alert($location.absUrl());
            if (window.location.href.search(/identity\=1/) > 0) {
                /**健康管家入口 */
                $rootScope.identityTo = 1
            } else if (window.location.href.search(/inviteId\=/) > 0) {
                /**邀请注册*/
                var inviteId = window.location.href.substring(window.location.href.indexOf("inviteId=") + 9);
                $rootScope.inviterUserId = parseInt(inviteId);
                $rootScope.identityTo = 1;
            } else {
                $rootScope.identityTo = 0;
            }

            if (window.location.href.search(/\#/) > 0) {
                /*打开微信转发url的处理*/
                var i = window.location.href.search(/\?/);
                var i2 = window.location.href.search(/\#/);
                if (i > 0 && i2 > 0 && i < i2) {
                    var absurl2 = window.location.href.split('#/')[1]
                    if (absurl2) {
                        window.location.href = APPSRC + '#/' + absurl2;
                        LoginService.wxAutoLogin(absurl2);
                    } else {
                        window.location.href = APPSRC + '#/';
                        LoginService.wxAutoLogin();
                    }
                } else {
                    if ($location.url() != "/about") {
                        LoginService.wxAutoLogin($location.url());
                    }
                }
            } else {
                LoginService.wxAutoLogin();
            }
        }

        if (true) {
            goLogin()
        } else {
            testLogin();
        }

        $rootScope.defaultHeadPic = "common/img/user/headPic-2.png";
        $rootScope.servicePic = "common/img/user/servicePic.png";
        /**测试环境下登录数据     */
        function testLogin() {
              $rootScope.showReady = true;
            $rootScope.identity = 1; //非微信浏览器测试时用
            $rootScope.userId = 2;//非微信浏览器测试时用
            $rootScope.user = {
                id: 2,
                nickname: 'syuenkei',
                //  telephone:13548953846,
                avatar: 'http://wx.qlogo.cn/mmopen/vi_32/ibytib2JRtibvnkl1iaaKLL3l5TtczMNicMtAVic0k2WRVDTmUzNldSVeVGGCbOoG4E1XnzA6Mich3FL5L5PcLsRAibibJA/0',
                digests: 'bababa',
                userType: 0,
                address: "zhzehsiwodejia",
                // telephone: '13548953846',
                token: 'o5-cgwMPIsWE--CpJo0d_nSeElmc',
            };
            $fetch.fetch(
                '/develop/simulation/login',
                {
                    params: { telephone: 13548953846 },
                }
            ).then(function (data) {
                $timeout(function () {
                    // console.log(data);
                    $rootScope.token = data;
                    ipCookie('token', data, ipCookiesParam);
                    $rootScope.showReady = true;
                    // alert(ipCookie('token'))
                })

            }).catch(function (e) {
                modalService.mainMsgError(e.msg);
            })
        }
        //url、路由监听
        var locationChangeSuccessOff = $rootScope.$on('$locationChangeSuccess', locationChangeSuccess);
        function locationChangeSuccess(event) {
            urlCgService.watch();
            $timeout(function () {
                if($rootScope.path1=='product' && !$rootScope.path2){
                    $init.title("产品");
                    return;
                }
                if ($rootScope.path1 == 'productDetail' && $rootScope.path2 == 'reservation') {
                    $init.title('客户预约');
                    return
                }
                if ($rootScope.path1 == 'productDetail' && $rootScope.path2 == 'reservation') {
                    $init.title('客户预约');
                    return
                }

                if ($rootScope.path1 == 'clientList' && $rootScope.path2 == 'info') {
                    $init.title('添加客户');
                    return
                }
                if ($rootScope.path1 == 'clientDetail' && $rootScope.path2 == 'editRelation') {
                    $init.title('添加关系');
                    return
                }
                if ($rootScope.path1 == 'clientDetail' && $rootScope.path2 == 'selectProduct') {
                    $init.title('添加产品');
                    return
                }
                if ($rootScope.path1 == 'clientDetail' && $rootScope.path2 == 'editInfo') {
                    $init.title('编辑客户');
                    return
                }
                if ($rootScope.path1 == 'team' && $rootScope.path2 == 'members') {
                    $init.title('团队成员');
                    return
                }
                if ($rootScope.path1 == 'shopcart' && $rootScope.path2 == 'orderPay' && !$rootScope.path3) {
                    $init.title('订单');
                    return
                }
                if ($rootScope.path1 == 'shopcart' && $rootScope.path2 == 'orderPay' && $rootScope.path3 == 'client') {
                    $init.title('客户');
                    return
                }
                if ($rootScope.path1 == 'shopcart' && $rootScope.path2 == 'orderPay' && $rootScope.path3 == 'coupon') {
                    $init.title('优惠券');
                    return
                }
                if ($rootScope.path1 == 'team' && $rootScope.path2 == 'create') {
                    $init.title('创建团队');
                    return
                }


            })
        }
        /**配合sroll-down2解决ios中滚动出界后上拉下滑出现黑底 然后不滚动的问题 */
        document.body.addEventListener('touchmove', function (evt) {
            //In this case, the default behavior is scrolling the body, which
            //would result in an overflow.  Since we don't want that, we preventDefault.
            if (!evt._isScroller) {
                //alert(evt._isScroller)
                evt.preventDefault();
            }
        }, false);
        /**解决android中键盘挡住输入框问题 */
        window.addEventListener("resize", function () {
            if (document.activeElement.tagName == "INPUT" || document.activeElement.tagName == "TEXTAREA") {
                window.setTimeout(function () {
                    document.activeElement.scrollIntoViewIfNeeded();
                }, 0);
            }
        })
        /**监听popstate 用于物理返回键改变路由 */
        window.addEventListener('popstate', function () {
            // console.log("popstate===changeState==" + $rootScope.changeState + "==" + $rootScope.changeStateName)
            if ($rootScope.changeState) {
                changeState();
            }
        })
        function changeState() {
            if ($rootScope.changeStateName == "index") {
                var state = {};
                var url = '#';
                pushState(state, url);
            } else if ($rootScope.changeStateName == "close") {
                // alert("你要关闭我");
                WeixinJSBridge.call('closeWindow');
            }else if ($rootScope.changeStateName == 'user'){
                var state = {};
                var url = '#/user';
                pushState(state, url);
            } else if($rootScope.changeStateName == 'support'){
                var state = {};
                var url = '#/support';
                pushState(state, url);
            }
        }
        function pushState(state, url) {
            history.pushState(state, null, url)
            $rootScope.changeState = null;
            $rootScope.changeStateName = null;
        }
        function replaceState(state,url){
            history.replaceState(state, null, url);
            $rootScope.changeState = null;
            $rootScope.changeStateName = null;
        }
    }]);
